package page;

import utils.JdbcUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class Page {
    public static int sum= JdbcUtils.JdbcQuery("SELECT page_name FROM page").size();  //注册文章数目统计

    public static void jssum(){
        sum=JdbcUtils.JdbcQuery("SELECT page_name FROM page").size();  //注册文章数目统计
    }
    public static String sql_add="INSERT INTO page values (?,?,?,?,?,?,?,?);";
    public static String sql_query_id="select * from page where page_name = ?";
    public static String sql_query_class_sum="select * from page where page_class = ?";
    public static String sql_updata_class="UPDATE page SET page_class = ? WHERE page_class = ? ";
    public static String sql_delete_name="DELETE FROM page WHERE page_name = ?";
    public static String sql_delete_class="DELETE FROM page WHERE page_class = ?";

    public static boolean sameName(String name){
        String sql_same_query="SELECT page_name FROM page WHERE page_name = "+name;
        return !JdbcUtils.JdbcQuery(sql_same_query).isEmpty();
    }
    public static void idChange(){
        String sql = "SELECT * FROM page ORDER BY page_id ASC";
        List<Map<String, Object>> rs = JdbcUtils.JdbcQuery(sql);
        int newId = 1;
        for(Map<String,Object> map : rs){
            for(String key : map.keySet()) {
                if (key.equals("page_id")){
                    int oldId = (int) map.get(key);
                    String updateSql = ("UPDATE page SET page_id = " + newId +" WHERE page_id = " + oldId);
                    JdbcUtils.JdbcUpdate(updateSql);
                    newId++;
                }
            }
        }
    }
}
